<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="box">
        <div class="all" v-if="list.length === 0">购物车空空如也</div>
        <template v-else>
            <div class="all"><input type="checkbox" v-model="isChecked">全选|全不选</div>
                <ul>
                    <li v-for="(item,index) in list" :key="item.id">
                        <div>
                            <input type="checkbox" v-model="checkGroup" :value="item" @change="isAllCheck()"/>
                        </div>
                        <div>
                            <img :src="item.pic" alt="">
                        </div>
                        <div>
                            <div>名称:{{item.name}}</div>
                            <div>价格：￥{{item.price}}</div>
                        </div>
                        <div>
                            <button @click="item.number--" :disabled="item.number===1">-</button>
                            <span>{{item.number}}</span>
                            <button @click="item.number++" :disabled="item.number===item.limit">+</button>
                        </div>
                        <div>
                            <button @click="deleteGood(index,item.id)">删除</button>
                        </div>
                    </li>
                </ul>
                <div class="all">总金额: {{totalPrice()}}</div>
        </template>      
    </div>
    <script>
        var vm = new Vue({
            el:"#box",
            data:{
                isChecked:false,
                checkGroup:[],
                list:[]
            },
            methods:{
                totalPrice(){
                    var total = 0;
                    this.checkGroup.forEach((ele)=>{
                        total += ele.price * ele.number;
                    })
                    return total
                },
                deleteGood(index,goodId){
                    this.list.splice(index,1);
                    this.checkGroup = this.checkGroup.filter(ele => ele.id !== goodId)
                    this.isAllCheck();
                },
                isAllCheck(){
                    if(this.checkGroup.length === this.list.length){
                        this.isChecked = true;
                    }else{
                        this.isChecked = false;
                    }
                }
            },
            watch:{
                isChecked(val){
                    if(val){
                        this.checkGroup = this.list
                    }else{
                        if(this.checkGroup.length === this.list.length)
                        this.checkGroup = [];
                    }
                }
            }
        })
    </script>
    
</body>
</html>